package com.sgb.kjwl.viewmodel.manage

import androidx.lifecycle.MutableLiveData
import com.sgb.businessmodule.BusinessConfig
import com.sgb.businessmodule.entity.BusinessRequestEntity
import com.sgb.businessmodule.entity.BusinessRequestEntity.SortFieldBean
import com.sgb.businessmodule.entity.HomeBusinessListEntity
import com.swgk.core.base.BaseViewModel
import com.swgk.core.base.api.ICallBack
import com.swgk.core.base.model.entity.BaseEntity
import com.swgk.core.base.model.entity.ListResponse
import retrofit2.Call
import kotlin.collections.ArrayList

class SubscribeBusinessViewModel : BaseViewModel() {

    private var page = 1

    var businessList: MutableLiveData<List<HomeBusinessListEntity>> = MutableLiveData()

    /*
     * 获取自主订阅列表数据
     * */
    fun getSubscribeBusinessListData(isLoadMore: Boolean) {
        val entity = BusinessRequestEntity()
        entity.page = if (!isLoadMore) 1 else page + 1
        entity.limit = if (isLoadMore) 10 else 20
        entity.cityNo = ArrayList()
        entity.isOnlySubscriptionInformation = true
        val bean = SortFieldBean()
        bean.publishTime = false
        entity.sortField = bean
        BusinessConfig.getInstance().api.getHomeBusinessData(entity).enqueue(object :
            ICallBack<BaseEntity<ListResponse<List<HomeBusinessListEntity>>>>(true, true) {
            override fun onSuccess(data: BaseEntity<ListResponse<List<HomeBusinessListEntity>>>) {
                if (data.data.list != null) {
                    businessList.postValue(data.data.list)
                } else {
                    businessList.postValue(ArrayList())
                }
            }

            override fun onFailure(
                call: Call<BaseEntity<ListResponse<List<HomeBusinessListEntity>>>>,
                t: Throwable
            ) {
                businessList.postValue(ArrayList())
                super.onFailure(call, t)
            }
        })
    }

}